﻿using Candy.Model;
using Candy.Web.Data;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Candy.ViewModel
{
    public class GeneReferenceEditViewModel : GeneAnnotationEditViewModel
    {
        public string Title
        {
            get
            {
                if (!string.IsNullOrEmpty(Entity.Value))
                    return base.Entity.Value.Split("\t".ToCharArray())[0];
                else
                    return "";
            }
            set
            {
                string[] arr = new string[2];
                if (!string.IsNullOrEmpty(Entity.Value))
                    arr = base.Entity.Value.Split("\t".ToCharArray());

                arr[0] = value.Trim();
                base.Entity.Value = arr[0] + "\t" + arr[1];
                RaisePropertyChanged("Title");
            }
        }

        public string Link
        {
            get
            {
                if (!string.IsNullOrEmpty(Entity.Value))
                    return base.Entity.Value.Split("\t".ToCharArray())[1];
                else
                    return "";
            }
            set
            {
                try
                {
                    Uri test = new Uri(value, UriKind.Absolute); //throws an exception if invalid url
                }
                catch (Exception ex)
                {
                    Entity.ValidationErrors.Add(new System.ComponentModel.DataAnnotations.ValidationResult("Invalid URL format", new List<string> { "Value" }));
                    throw (ex);
                }
                string[] arr = new string[2];
                if (!string.IsNullOrEmpty(Entity.Value))
                    arr = base.Entity.Value.Split("\t".ToCharArray());

                arr[1] = value.Trim();
                base.Entity.Value = arr[0] + "\t" + arr[1];
                RaisePropertyChanged("Link");
            }
        }



        public GeneReferenceEditViewModel(IDataService dataService, GeneAnnotationDto dto)
            : base(dataService, dto)
        {
            dto.PropertyChanged += dto_PropertyChanged;
        }

        void dto_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RaisePropertyChanged("Title");
            RaisePropertyChanged("Link");
        }
    }
}
